package com.ep.thirdPlatformProject.oa.service.impl;

import cn.com.weaver.services.webservices.WorkflowServiceHttpBindingStub;
import cn.com.weaver.services.webservices.WorkflowServiceLocator;
import com.ep.thirdPlatformProject.oa.service.HrmResourceService;
import com.ep.thirdPlatformProject.system.config.oa.WorkflowFormProperties;
import com.ep.thirdPlatformProject.oa.enums.ResultEnum;
import com.ep.thirdPlatformProject.oa.service.WorkFlowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import weaver.hrm.webservice.UserBean;
import weaver.workflow.webservices.WorkflowRequestInfo;

import java.net.URL;


/**
 * 工作流服务impl
 *
 * @author Karl
 * @date 2024/05/28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WorkFlowServiceImpl implements WorkFlowService {

    private final WorkflowFormProperties workflowFormProperties;
    private final HrmResourceService hrmResourceService;

    /**
     * 创建工作流请求
     *
     * @param workflowRequestInfo 工作流请求信息
     * @param userId              用户id
     * @return {@link String }
     */
    @Override
    public String doCreateWorkflowRequest(WorkflowRequestInfo workflowRequestInfo, Integer userId) {
        try {
            WorkflowServiceLocator service = new WorkflowServiceLocator();

            WorkflowServiceHttpBindingStub stub = new WorkflowServiceHttpBindingStub(new URL(workflowFormProperties.getWorkflowUrl()), service);
            String result = stub.doCreateWorkflowRequest(workflowRequestInfo, userId);
            if (result != null && Integer.parseInt(result) > 0) {
                // 返回流程的id
                return result;
            } else {
                String resultByCode = ResultEnum.getResultByCode(result);
                throw new RuntimeException("创建流程失败：" + resultByCode);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("保存时错误:" + e.getMessage(), e);
        }
    }


    /**
     * 提交工作流请求
     *
     * @param workflowRequestInfo 工作流请求信息
     * @param requestId           请求id
     * @param userId              用户id
     * @param type                类型 1
     * @param remark              备注
     * @return {@link String }
     */
    @Override
    public String submitWorkflowRequest(WorkflowRequestInfo workflowRequestInfo, Integer requestId, Integer userId, String type, String remark) {
        try {
            WorkflowServiceLocator service = new WorkflowServiceLocator();

            WorkflowServiceHttpBindingStub stub = new WorkflowServiceHttpBindingStub(new URL(workflowFormProperties.getWorkflowUrl()), service);

            return stub.submitWorkflowRequest(workflowRequestInfo, requestId, userId, type, remark);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException("提交错误：" + e.getMessage(), e);
        }

    }

    /**
     * 第二个构建表单请求
     *
     * @param requestInfo 请求信息
     * @return {@link String }
     */
    @Override
    public String secondBuildFormRequest(WorkflowRequestInfo requestInfo, Integer userId) {
        try {
            String result = doCreateWorkflowRequest(requestInfo, userId);
            log.info("第二步创建工作流结果：{}", result);
            return result;
        } catch (Exception e) {
            log.error("第二步创建工作流错误:{}", e.getMessage(), e);
            throw new RuntimeException("第二步创建工作流错误：" + e.getMessage(), e);
        }
    }

    /**
     * 第三提交表单请求
     *
     * @param requestInfo 请求信息
     * @param requestId   请求id
     * @return {@link String }
     */
    @Override
    public String thirdSubmitFormRequest(WorkflowRequestInfo requestInfo, int requestId) {
        try {
            requestInfo.setRequestId(String.valueOf(requestId));
            String result = submitWorkflowRequest(requestInfo, requestId, workflowFormProperties.getCreator(), "1", "OK");
            log.info("第三步提交工作流,工作流id:{},结果：{}", requestId, result);
            return result;
        } catch (Exception e) {
            log.error("第三部提交工作流错误:{}", e.getMessage(), e);
            throw new RuntimeException("第三步提交工作流错误：" + e.getMessage(), e);
        }
    }

    @Override
    public String getWorkflowTodoList(String userNo) {
        try {



            return "";
        } catch (Exception e) {
            log.error("第三部提交工作流错误:{}", e.getMessage(), e);
            throw new RuntimeException("第三步提交工作流错误：" + e.getMessage(), e);
        }

    }
}
